﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;

using Microsoft.SharePoint.Client;

using SBPweb.RIA.Client.Common.Collections;

namespace SBPweb.Sharepoint.Client.Controls.Fields
{

    public partial class UserEditSettings : ComboBoxEditSettings, IFieldEditSettings<FieldUser>
    {
        
        public UserEditSettings()
            : base()
        {
            this.DisplayMember = "LookupValue";
            this.ValueMember = "LookupId";
        }

        public static readonly DependencyProperty FieldProperty = DependencyProperty.Register("Field", typeof(FieldUser), typeof(IFieldEditSettings<FieldUser>), null);
        public static readonly DependencyProperty UsersSourceProperty = DependencyProperty.Register("UsersSource", typeof(ObservableList<FieldUserValue>), typeof(ComboBoxEditSettings), null);

        public ClientContext SharepointContext
        {
            get;
            set;
        }

        public FieldUser Field
        {
            get
            {
                return GetValue(FieldProperty) as FieldUser;
            }
            set
            {
                SetValue(FieldProperty, value);
                InitializeFromField();
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Field"));
                }
            }
        }

        public ObservableList<FieldUserValue> UsersSource
        {
            get
            {
                return GetValue(UsersSourceProperty) as ObservableList<FieldUserValue>;
            }
            set
            {
                SetValue(UsersSourceProperty, value);
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("UsersSource"));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void InitializeFromField()
        {

        }

        public override DevExpress.Xpf.Editors.IBaseEdit CreateEditor(bool assignEditorSettings, IDefaultEditorViewInfo defaultViewInfo)
        {
            DevExpress.Xpf.Editors.ComboBoxEdit ret = base.CreateEditor(assignEditorSettings, defaultViewInfo) as DevExpress.Xpf.Editors.ComboBoxEdit;

            Binding binding = new Binding("UsersSource");
            binding.Source = this;
            ret.SetBinding(DevExpress.Xpf.Editors.ComboBoxEdit.ItemsSourceProperty, binding);

            return ret;
        }
        //    //editor = new UserTagEditor();
        //    //if (assignEditorSettings)
        //    //{
        //    //    AssignToEdit(editor, defaultViewInfo);
        //    //}
        //    editor = base.CreateEditor(assignEditorSettings, defaultViewInfo) as ComboBoxEdit;
        //    //Binding binding = new Binding("Column");
        //    //binding.Converter = new UserTagItemsConverter();
        //    //binding.ConverterParameter = this.Name;
        //    //editor.SetBinding(ComboBoxEdit.ItemsSourceProperty, binding);
        //    editor.Loaded += new RoutedEventHandler(editor_Loaded);
        //    return editor;
        //}

        //void editor_Loaded(object sender, RoutedEventArgs e)
        //{
        //    //if (editor.EditValue != null)
        //    //{
        //    //    editor.ItemsSource = new FieldUserValue[] { editor.EditValue as FieldUserValue };
        //    //}
        //    //editor.Items.Add(editor.DataContext
        //}

    }

    //public class UserTagItemsConverter : IValueConverter
    //{

    //    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    //    {
    //        //RowPropertyValueConverter rowValueConverter = new RowPropertyValueConverter();
    //        //var obj = (rowValueConverter as IValueConverter).Convert(value, null, parameter, culture);

    //        //FieldUserValue fuv = value as FieldUserValue;
    //        //if (fuv != null)
    //        //{
    //        //    if (string.IsNullOrEmpty(parameter as string))
    //        //    {
    //        //        parameter = Attribute;
    //        //    }
    //        //    switch (parameter as string)
    //        //    {
    //        //        default:
    //        //            break;
    //        //        case "LookupId":
    //        //            return fuv.LookupId;
    //        //        case "LookupValue":
    //        //            return fuv.LookupValue;
    //        //    }
    //        //}
    //        return null;
    //    }

    //    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    //    {
    //        return null;
    //    }

    //}

}
